<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Data extends CI_Controller {
  public function __construct(){
    parent::__construct();
    $this->load->model('Data_model','m');//load model dan singkat dengan m
    $this->load->helper('url');
	  $this->load->library('session');
  }

  
  public function index()
  {
    //load library table agar mudah membuat tabel
    $this->load->library('table');
    $this->table->set_heading('ID', 'ID TPS', 'ID Cakada', 'ID User','Hasil');

    $all_data = $this->m->all();//return array of object
    foreach($all_data as $data){
      $this->table->add_row(
        anchor('data/view/'.$data->id,$data->id),
        anchor('tps/view/'.$data->id_tps,$data->id_tps),
        anchor('cakada/view/'.$data->id_cakada,$data->id_cakada),
        anchor('user/view/'.$data->id_user,$data->id_user),
        $data->hasil,
        //anchor('data/view/'.$data->id,'Lihat'),
        anchor('data/edit/'.$data->id,'Edit')
      );
    }
    echo $this->table->generate();
  }



  public function add()
  {
    //untuk memudahkan membuat form
    $this->load->helper('form');
    //load library table agar mudah membuat tabel
    $this->load->library('table');

    //buka form
    echo form_open('data/save');

    $options = $this->m->getIdNama('tps');
    $this->table->add_row('TPS',':',form_dropdown('id_tps', $options));

    $options = $this->m->getIdNama('cakada');
    $this->table->add_row('Calon',':',form_dropdown('id_cakada', $options));

    $options = $this->m->getIdNama('user');
    $this->table->add_row('Operator',':',form_dropdown('id_user', $options));

    $this->table->add_row('Hasil',':',form_input('hasil'));

    //tombol tambah
    $this->table->add_row(form_submit('tambah', 'Tambah'));
    $this->table->add_row(anchor('data', 'Kembali'));
    echo $this->table->generate();

    //tutup form
    echo form_close();
  }

  //save our new data
  public function save()
  {
    //save 
    $data['id_tps']=(int)$this->input->post('id_tps');
    $data['id_cakada']=(int)$this->input->post('id_cakada');
    $data['id_user']=(int)$this->input->post('id_user');
    $data['hasil']=(int)$this->input->post('hasil');

    //simpan id autoincrement yang dihasilkan untuk ditampilkan jika sukses disimpan
    $id = $this->m->insert($data);
		redirect('data/view/'.$id);

  }

  public function view($id=1)
  {
    $this->load->library('table');
    $o = $this->m->get($id);
    $this->table->add_row('id',':', $o->id);
    $this->table->add_row('created_at',':', $o->created_at);
    $this->table->add_row('updated_at',':', $o->updated_at);
    $this->table->add_row('TPS',':',$o->id_tps);
    $this->table->add_row('Calon',':',$o->id_cakada);
    $this->table->add_row('Operator',':',$o->id_cakada);
    $this->table->add_row('Hasil',':',$o->id_cakada);
    $this->table->add_row(anchor('data/add','Add'),anchor('data/edit/'.$id,'Edit'));
    echo $this->table->generate();
  }

  public function edit($id)
  {
    $this->load->library('table');
    $this->load->helper('form');
    $o = $this->m->get($id);

    echo form_open('data/update/'.$o->id);
    //push the table
    $this->table->add_row('id',':', $o->id);
    $this->table->add_row('created_at',':', $o->created_at);
    $this->table->add_row('updated_at',':', $o->updated_at);

    $options = $this->m->getIdNama('tps');
    $this->table->add_row('TPS',':',form_dropdown('id_tps', $options,$o->id_tps));

    $options = $this->m->getIdNama('cakada');
    $this->table->add_row('Calon',':',form_dropdown('id_cakada', $options,$o->id_cakada));

    $options = $this->m->getIdNama('user');
    $this->table->add_row('Operator',':',form_dropdown('id_user', $options,$o->id_user));

    $this->table->add_row('Hasil',':',form_input('hasil',$o->hasil));


    $this->table->add_row(form_submit('update', 'Update'));
    $this->table->add_row(anchor('data', 'Kembali'));

    //now render the table
    echo $this->table->generate();
    echo form_close();


  }

  public function update($id){
    //update
    $data['id_tps']=(int)$this->input->post('id_tps');
    $data['id_cakada']=(int)$this->input->post('id_cakada');
    $data['id_user']=(int)$this->input->post('id_user');
    $data['hasil']=(int)$this->input->post('hasil');

    $this->m->update($id,$data);
		redirect('data/view/'.$id);

  }

}
